home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTE16 / STREAM.C < prev    next >
C/C++ Source or Header  |  1996-04-29  |  11KB  |  338 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "stream.h"
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName = "MyApp";
  19. LPCTSTR lpszTitle   = "MIDI Stream"; 
  20.  
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    hWnd = CreateWindow( lpszAppName, 
  54.                         lpszTitle,    
  55.                         WS_OVERLAPPEDWINDOW | WS_VSCROLL, 
  56.                         CW_USEDEFAULT, 0, 
  57.                         CW_USEDEFAULT, 0,  
  58.                         NULL,              
  59.                         NULL,              
  60.                         hInstance,         
  61.                         NULL               
  62.                       );
  63.  
  64.    if ( !hWnd ) 
  65.       return( FALSE );
  66.  
  67.    ShowWindow( hWnd, nCmdShow ); 
  68.    UpdateWindow( hWnd );         
  69.  
  70.    while( GetMessage( &msg, NULL, 0, 0) )   
  71.    {
  72.       TranslateMessage( &msg ); 
  73.       DispatchMessage( &msg );  
  74.    }
  75.  
  76.    return( msg.wParam ); 
  77. }
  78.  
  79.  
  80. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  81. {
  82.     WNDCLASSEX wcex;
  83.  
  84.    wcex.style         = lpwc->style;
  85.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  86.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  87.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  88.    wcex.hInstance     = lpwc->hInstance;
  89.    wcex.hIcon         = lpwc->hIcon;
  90.    wcex.hCursor       = lpwc->hCursor;
  91.    wcex.hbrBackground = lpwc->hbrBackground;
  92.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  93.    wcex.lpszClassName = lpwc->lpszClassName;
  94.  
  95.    // Added elements for Windows 95.
  96.    //...............................
  97.    wcex.cbSize = sizeof(WNDCLASSEX);
  98.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  99.                             IMAGE_ICON, 16, 16,
  100.                             LR_DEFAULTCOLOR );
  101.             
  102.    return RegisterClassEx( &wcex );
  103. }
  104.  
  105.  
  106. #define USR_SHORTMSG  (WM_USER+1)
  107.  
  108. #define MSG_LEN          1024
  109. #define DATABLOCK_SIZE   1024L
  110.  
  111. char       msg[MSG_LEN+1];
  112.  
  113. HWND       hListBox = NULL;
  114. MMRESULT   rc;
  115.  
  116. UINT       nDevId = 1;
  117. HMIDISTRM  hms    = NULL;
  118. MIDIHDR*   pmh    = NULL;
  119.  
  120. typedef struct
  121. {
  122.    DWORD ms;        // time in milliseconds
  123.    DWORD dwMidiMsg; // short MIDI message
  124. } MIDIMSG;
  125.  
  126. #define SONG_LEN  6 
  127.  
  128. MIDIEVENT MidiEvent[SONG_LEN] =
  129.    {  0, 0, MEVT_F_SHORT | MEVT_F_CALLBACK | 0x403c90, 0,  // C on  (C3)
  130.      10, 0, MEVT_F_SHORT | MEVT_F_CALLBACK | 0x003c90, 0,  // C off
  131.      10, 0, MEVT_F_SHORT | MEVT_F_CALLBACK | 0x404c90, 0,  // G on
  132.      10, 0, MEVT_F_SHORT | MEVT_F_CALLBACK | 0x004c90, 0,  // G off
  133.      10, 0, MEVT_F_SHORT | MEVT_F_CALLBACK | 0x404090, 0,  // E on
  134.      10, 0, MEVT_F_SHORT | MEVT_F_CALLBACK | 0x004090, 0   // E off
  135.    };
  136.  
  137.  
  138. /*****************************************************************************
  139. *  WndProc
  140. *
  141. *
  142. *
  143. ******************************************************************************/
  144.  
  145. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  146. {
  147.    switch( uMsg )
  148.    {
  149.       case WM_CREATE :
  150.               // allocate MIDIHDR buffer block
  151.                //..............................
  152.  
  153.               pmh = HeapAlloc( GetProcessHeap(),                         
  154.                                 HEAP_ZERO_MEMORY,                         
  155.                                 sizeof(MIDIHDR) );                        
  156.               if (pmh)                                                  
  157.               {                                                            
  158.                   pmh->lpData = HeapAlloc( GetProcessHeap(),             
  159.                                            HEAP_ZERO_MEMORY,             
  160.                                            DATABLOCK_SIZE);                       
  161.                   pmh->dwBufferLength = DATABLOCK_SIZE;
  162.               }
  163.  
  164.               // Create ListBox
  165.               //...............
  166.  
  167.               hListBox = CreateWindow( "LISTBOX", "",    
  168.                                        WS_CHILD | LBS_NOTIFY | 
  169.                                        WS_VSCROLL | WS_BORDER | 
  170.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  171.                                        0, 0, 
  172.                                        0, 0,  
  173.                                        hWnd,              
  174.                                        (HMENU)101,              
  175.                                        hInst,         
  176.                                        NULL );
  177.               break;
  178.  
  179.       case WM_SIZE :
  180.               MoveWindow( hListBox, 0, 0, 
  181.                           LOWORD( lParam ), 
  182.                           HIWORD( lParam ), TRUE );
  183.               break;
  184.  
  185.       case WM_COMMAND :
  186.               switch( LOWORD( wParam ) )
  187.               {
  188.                  case IDM_TEST:
  189.                         {
  190.                            MIDIPROPTEMPO   mpt;
  191.                            MIDIPROPTIMEDIV mptd;
  192.                            
  193.                            // open MIDI stream
  194.                            //.................
  195.                            
  196.                            rc = midiStreamOpen(&hms, &nDevId, 1,    
  197.                                                (DWORD)hWnd, (DWORD)NULL, CALLBACK_WINDOW);
  198.  
  199.                            if (rc != MMSYSERR_NOERROR)
  200.                            {
  201.                                midiOutGetErrorText(rc, msg, MSG_LEN);
  202.                                MessageBox(hWnd, msg, NULL, MB_OK);
  203.                                break;
  204.                            }
  205.  
  206.                            // pause playback of MIDI stream
  207.                            //..............................
  208.  
  209.                            midiStreamPause(hms);
  210.  
  211.                            // display default stream properties
  212.                            //..................................
  213.  
  214.                            mpt.cbStruct  = sizeof(MIDIPROPTEMPO);
  215.                            mptd.cbStruct = sizeof(MIDIPROPTIMEDIV);
  216.  
  217.                            midiStreamProperty(hms, (LPBYTE)&mpt,  MIDIPROP_GET | MIDIPROP_TEMPO);
  218.                            midiStreamProperty(hms, (LPBYTE)&mptd, MIDIPROP_GET | MIDIPROP_TIMEDIV);
  219.                            
  220.                            sprintf(msg, "Tempo:%ld  Time Div:%ld",
  221.                                    mpt.dwTempo, mptd.dwTimeDiv);
  222.                            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)msg); 
  223.  
  224.                            // Load data into MIDIHDR
  225.                            //.......................
  226.  
  227.                            pmh->dwBytesRecorded = SONG_LEN * sizeof(MIDIEVENT);
  228.                            memcpy(pmh->lpData, MidiEvent, pmh->dwBytesRecorded);
  229.  
  230.                            // send MIDI stream to MIDI output device and
  231.                            // restart playback 
  232.                            //...........................................
  233.  
  234.                            rc = midiOutPrepareHeader(hms, pmh, sizeof(MIDIHDR));
  235.  
  236.                            if (rc == MMSYSERR_NOERROR)
  237.                                rc = midiStreamRestart(hms);
  238.  
  239.                            if (rc == MMSYSERR_NOERROR)
  240.                                rc = midiStreamOut(hms, pmh, sizeof(MIDIHDR));
  241.                            
  242.                            if (rc != MMSYSERR_NOERROR)
  243.                            {
  244.                                midiOutGetErrorText(rc, msg, MSG_LEN);
  245.                                MessageBox(hWnd, msg, NULL, MB_OK);
  246.                                break;
  247.                            }
  248.  
  249.                            // call midiStreamPosition() and display current position
  250.                         }
  251.                         break;
  252.  
  253.                  case IDM_ABOUT :
  254.                         DialogBox( hInst, "AboutBox", hWnd, About );
  255.                         break;
  256.  
  257.                  case IDM_EXIT :
  258.                         DestroyWindow( hWnd );
  259.                         break;
  260.               }
  261.               break;
  262.  
  263.       case MM_STREAM_DONE:
  264.       case MM_MOM_DONE:
  265.               {
  266.                  // unprepeare the data buffer block
  267.                  // and close the MIDI output device
  268.                  //.................................
  269.  
  270.                  midiOutUnprepareHeader(hms, pmh, sizeof(MIDIHDR));
  271.                  midiStreamStop(hms);
  272.                  midiStreamClose(hms);
  273.               }
  274.               break;
  275.  
  276.       case WM_DESTROY :
  277.               // unprepeare the data buffer block
  278.               // and close the MIDI output device
  279.               //.................................
  280.  
  281.               midiOutUnprepareHeader(hms, pmh, sizeof(MIDIHDR));
  282.               midiStreamStop(hms);
  283.               midiStreamClose(hms);
  284.               
  285.               // free the MIDIHDR buffer block
  286.                //..............................
  287.  
  288.               if (pmh != NULL)
  289.               {
  290.                   HeapFree(GetProcessHeap(), 0, pmh->lpData);                 
  291.                   HeapFree(GetProcessHeap(), 0, pmh);
  292.                   pmh = NULL;
  293.                }
  294.  
  295.               PostQuitMessage(0);
  296.               break;
  297.  
  298.       default :
  299.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  300.    }
  301.  
  302.    return( 0L );               
  303. }
  304.  
  305.  
  306.  
  307. LRESULT CALLBACK About( HWND hDlg,           
  308.                         UINT message,        
  309.                         WPARAM wParam,       
  310.                         LPARAM lParam)
  311. {
  312.    switch (message) 
  313.    {
  314.        case WM_INITDIALOG: 
  315.                return (TRUE);
  316.  
  317.        case WM_COMMAND:                              
  318.                if (   LOWORD(wParam) == IDOK         
  319.                    || LOWORD(wParam) == IDCANCEL)    
  320.                {
  321.                        EndDialog(hDlg, TRUE);        
  322.                        return (TRUE);
  323.                }
  324.                break;
  325.    }
  326.  
  327.    return (FALSE); 
  328. }
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.